﻿
using System;
using SqlSugar;
using System.Threading.Tasks;
using System.Linq.Expressions;

using Oliving.Edge.Model;
using Oliving.Edge.Model.Dto;
using Oliving.Edge.Model.Models;
using Oliving.Edge.IRepository;
using Oliving.Edge.IRepository.UnitOfWork;
using Oliving.Edge.Repository.Base;
using Oliving.Edge.Model.Dto.User;
using Oliving.Edge.IRepository.User;
using Oliving.Edge.Model.Models.User;
using System.Collections.Generic;
using Oliving.Edge.Model.Models.XLT;

namespace Oliving.Edge.Repository.User
{
    /// <summary>
    /// Sys_DeptRepository
    /// </summary>
    public class Sys_DeptRepository : BaseRepository<Sys_Dept>, ISys_DeptRepository
    {
        public Sys_DeptRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }






        /// <summary>
        /// 自定义分页查询
        /// </summary>
        /// <param name="whereExpression">查询条件</param>
        /// <param name="pageIndex" > 页码（下标0）</param>
        /// <param name="pageSize" > 页大小</param>
        /// <param name="sortField" > 排序字段</param>
        /// <param name="sortOrder" > 排序顺序（asc/desc）</param>
        /// <returns></returns>
        public async Task<PageModel<Sys_DeptPageDto>> QueryPageList(
                Expression<Func<Sys_Dept, bool>> whereExpression,
                int pageIndex = 1, int pageSize = 20,
                string sortField = null, string sortOrder = null
                )
        {


            #region 排序

            string strOrderByFileds = null;

            if (!string.IsNullOrEmpty(sortField))
            {
                strOrderByFileds = sortField;
                if (sortOrder != null && sortOrder.ToLower().Contains("desc"))
                {
                    strOrderByFileds = strOrderByFileds + " desc";
                }
                else
                {
                    strOrderByFileds = strOrderByFileds + " asc";
                }
            }

            #endregion

            #region 单表

            RefAsync<int> totalCount = 0;
            var list = await Db.Queryable<Sys_Dept>()
                     .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                     .WhereIF(whereExpression != null, whereExpression)
                     .Select<Sys_DeptPageDto>()
                     .ToPageListAsync(pageIndex, pageSize, totalCount);

            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / pageSize.ObjToDecimal())).ObjToInt();
            return new PageModel<Sys_DeptPageDto>()
            {
                dataCount = totalCount,
                pageCount = pageCount,
                page = pageIndex,
                PageSize = pageSize,
                data = list
            };

            #endregion




        }





        /// <summary>
        /// 
        /// </summary>
        /// <param name="whereExpression">条件</param>
        /// <param name="sortField" > 排序字段</param>
        /// <param name="sortOrder" > 排序顺序（asc/desc）</param>
        /// <returns></returns>
        public async Task<List<Sys_Dept_ComProPageDto>> GetList_ComPro(
                Expression<Func<Sys_Dept, bool>> whereExpression,
                string sortField = null, string sortOrder = null
                )
        {


            #region 排序

            string strOrderByFileds = null;

            if (!string.IsNullOrEmpty(sortField))
            {
                strOrderByFileds = sortField;
                if (sortOrder != null && sortOrder.ToLower().Contains("desc"))
                {
                    strOrderByFileds = strOrderByFileds + " desc";
                }
                else
                {
                    strOrderByFileds = strOrderByFileds + " asc";
                }
            }

            #endregion

            #region 单表
             
            var list = Db.Queryable<Sys_Dept, WY_Project, WY_Company>((_Sys_Dept, _WY_Project, _WY_Company) => new object[]
                           {
                                  JoinType.Left,_Sys_Dept.ProID==_WY_Project.Id,
                                  JoinType.Left,_Sys_Dept.ComID==_WY_Company.Id,
                            })
                            .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                            .WhereIF(whereExpression != null, whereExpression)
                            .Select((_Sys_Dept, _WY_Project, _WY_Company) => new Sys_Dept_ComProPageDto
                            {
                                ComID = _Sys_Dept.ComID,
                                Id = _Sys_Dept.Id,
                                Pid = _Sys_Dept.Pid,
                                ProID = _Sys_Dept.ProID,
                                DeptName = _Sys_Dept.DeptName,
                                CreateTime = _Sys_Dept.CreateDate,
                                ComName = _WY_Company.CompanyName,
                                ProName = _WY_Project.ProName
                            });

            return list.ToList();

            #endregion

        }






    }
}

